package com.store.app.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.store.app.ExitApplication;
import com.store.app.R;
import com.store.app.adapter.BookedViewPagerAdapter;
import com.store.app.fragment.SendStamps_1Fragment;
import com.store.app.fragment.TheOrderBuyFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 送券
 */
public class SendStampsActivity extends FragmentActivity implements View.OnClickListener {
    public List<Fragment> fragments = new ArrayList<Fragment>();
    private TextView radioGroup_titlebar_1, radioGroup_titlebar_2;
    private TextView title;
    private ViewPager viewpager;
    private TextView tv1;
    private TextView tv2;
    private TextView tvSellNumber;
    private String showFragmentIndex="0";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ExitApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_sendstamps);
        findView();
        setListener();
        initData();
        Intent intent=new Intent(SendStampsActivity.this,XiaopiaoDialogActivity.class);
        startActivity(intent);
    }

    private void setListener() {
        findViewById(R.id.public_ll_return).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void findView() {

        viewpager = (ViewPager) findViewById(R.id.booked_viewpager);
        radioGroup_titlebar_1 = (TextView) findViewById(R.id.booked_rb1_titleck1);
        radioGroup_titlebar_2 = (TextView) findViewById(R.id.booked_rb2_titleck2);

        tv1 = (TextView) findViewById(R.id.tv1);
        tv2 = (TextView) findViewById(R.id.tv2);
        tvSellNumber = (TextView) findViewById(R.id.tv_sell_number);
        title = (TextView) findViewById(R.id.tvTitle);
        title.setText("送券");
        tv1.setOnClickListener(this);
        tv2.setOnClickListener(this);
        if(tvSellNumber.getText().toString().trim().equals("0")){
            tvSellNumber.setVisibility(View.GONE);
        }
    }

    private void refreshUITab(String index) {
        if(index.equals("0")){
            tv1.setTextColor(Color.parseColor("#f96116"));
            tv2.setTextColor(Color.parseColor("#333333"));
            radioGroup_titlebar_1
                    .setBackgroundColor(getResources()
                            .getColor(R.color.booked_color));
            radioGroup_titlebar_2
                    .setBackgroundColor(getResources()
                            .getColor(R.color.transparent));

        }else if(index.equals("1")){
            tv2.setTextColor(Color.parseColor("#f96116"));
            tv1.setTextColor(Color.parseColor("#333333"));
            radioGroup_titlebar_1
                    .setBackgroundColor(getResources()
                            .getColor(R.color.transparent));
            radioGroup_titlebar_2
                    .setBackgroundColor(getResources()
                            .getColor(R.color.booked_color));
        }
    }

    private void initData() {
        fragments.add(new SendStamps_1Fragment());
        fragments.add(new TheOrderBuyFragment());
        BookedViewPagerAdapter disfratab = new BookedViewPagerAdapter(getSupportFragmentManager(), viewpager, fragments);
        viewpager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                refreshUITab(position+"");
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        refreshUITab(showFragmentIndex);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv1:
                showFragmentIndex="0";
                refreshUITab(showFragmentIndex);
                viewpager.setCurrentItem(0);
                break;
            case R.id.tv2:
                showFragmentIndex="1";
                refreshUITab(showFragmentIndex);
                viewpager.setCurrentItem(1);
                break;

        }
    }

}